#include "ExpAST.h"
#include "Globe.h"
#include <iostream>
#include "parser.h"
#include "Globe.h"
#pragma warning(disable:4267)
using std::cout;
using std::endl;

unique_ptr<BaseExpAST> parser::parseCommaExp()
{
    unique_ptr<BaseExpAST>l = this->parseAssignExp();
    if (this->lexer->next_token() == TOK::COMMA)
    {
        unique_ptr<CommaAST>ret(new CommaAST);
        ret->content.push_back(std::move(l));
        l = nullptr;
        while (this->lexer->next_token() == TOK::COMMA)
        {
            this->lexer->match(TOK::COMMA);
            ret->content.push_back(this->parseAssignExp());
        }
        return ret;
    }
    return l;
}

void CommaAST::Dump() const
{
    cout << "{\"type\":\"Comma\",\"content\":[";
    this->content[0]->Dump();
    for (int i = 1; i < this->content.size(); i++)
    {
        cout << ",";
        this->content[i]->Dump();
    }
    cout << "]}";
}


bool AddAST::is_lval() const
{
    if (this->op == DEFAULT)
        return this->exp1->is_lval();
    return false;
}

void AddAST::Analyze() const
{
    switch (this->op)
    {
    case ADD:
    case SUB:
        this->exp2->Analyze();
        GENASM(cmd_code::PUSH, CVR(EAX), cmd_val());
        this->exp1->Analyze();
        GENASM(cmd_code::POP, CVR(EDX), cmd_val());
        if (this->op == ADD) {
            GENASM(cmd_code::ADD, CVR(EAX), CVR(EDX));
        }
        else {
            GENASM(cmd_code::SUB, CVR(EAX), CVR(EDX));
        }
    default:
        break;
    }
}
